package com.xxwy.ddu.cms.manager.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xxwy.ddu.cms.authonrize.entity.BaseUser;
import com.xxwy.ddu.cms.authonrize.entity.MenuButton;
import com.xxwy.ddu.cms.authonrize.entity.MenuMain;
import com.xxwy.ddu.cms.authonrize.entity.Role;
import com.xxwy.ddu.cms.authonrize.service.IMenuButtonService;
import com.xxwy.ddu.cms.authonrize.service.IMenuMainService;
import com.xxwy.ddu.cms.authonrize.service.IRoleService;
import com.xxwy.ddu.cms.authonrize.service.IUserService;
import com.xxwy.ddu.cms.manager.entity.Log;
import com.xxwy.ddu.cms.manager.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author xxwy
 * on 2018/9/5 0005
 * 菜单页面的跳转之类的
 */
@Controller
public class MenuController {

    private static final String MANAGER = "manager";


    @Autowired
    IMenuMainService iMenuMainService;

    @Autowired
    IMenuButtonService iMenuButtonService;

    @Autowired
    ILogService iLogService;

    @Autowired
    IRoleService iRoleService;

    @Autowired
    IUserService iUserService;

    /**
     * 欢迎页
     *
     * @return
     */
    @GetMapping("/")
    public ModelAndView welcome() {
        ModelAndView view = new ModelAndView();
        view.setViewName("main");
        return view;
    }

    @GetMapping("index")
    public String index(ModelMap model, @AuthenticationPrincipal UserDetails userDetails) {
        //添加权限，
        if (userDetails != null) {
            //拿出权限去找他能访问什么页面
            String username = userDetails.getUsername();
            //找权限拿页面
            List<MenuMain> userMenuInfo = iMenuMainService.getUserMenuInfo(username);
            model.put("menu", userMenuInfo);
        }
        return "index";
    }

    @GetMapping("/main")
    public String main(ModelMap map) {
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.groupBy("create_time");
        EntityWrapper<Log> wrapper = new EntityWrapper<>();
        wrapper.orderBy("create_time",false);
        List<Log> logs = iLogService.selectList(wrapper);
        map.put("logs", logs);
        return "main";
    }

    @GetMapping("/userInfo")
    public String userInfo(ModelMap map, @AuthenticationPrincipal UserDetails userDetails) {
        String username = userDetails.getUsername();
        List<MenuButton> userButton = iMenuButtonService.getUserButton(username, 1);
        addDetail(map, userButton);
        //添加角色
        List<Role> roles = iRoleService.selectList(null);
        map.put("roles",roles);
        return MANAGER + "/userInfo";
    }

    private void addDetail(ModelMap map, List<MenuButton> userButton) {
        if (userButton != null) {
            boolean insert = false;
            boolean delete = false;
            boolean update = false;
            for (MenuButton menuButton : userButton) {
                if("3".equals(menuButton.getbType())){
                    update = true;
                }
                if("2".equals(menuButton.getbType())){
                    delete = true;
                }
                if("1".equals(menuButton.getbType())){
                    insert = true;
                }
            }
            if(!insert){
                map.put("insertReadonly", "layui-btn-disabled");
            }
            if(!update){
                map.put("updateReadonly", "layui-btn-disabled");
            }
            if(!delete){
                map.put("deleteReadonly", "layui-btn-disabled");
            }
        } else {
            map.put("insertReadonly", "layui-btn-disabled");
            map.put("deleteReadonly", "layui-btn-disabled");
            map.put("updateReadonly", "layui-btn-disabled");
        }
    }

    @GetMapping("/roleInfo")
    public String roleInfo(ModelMap map, @AuthenticationPrincipal UserDetails userDetails) {
        String username = userDetails.getUsername();
        List<MenuButton> userButton = iMenuButtonService.getUserButton(username, 2);
        addDetail(map, userButton);
        return MANAGER + "/roleInfo";
    }

    @GetMapping("/authorities")
    public String authoritiesInfo(ModelMap map, @AuthenticationPrincipal UserDetails userDetails) {
        String username = userDetails.getUsername();
        List<MenuButton> userButton = iMenuButtonService.getUserButton(username, 3);
        addDetail(map, userButton);
        List<Role> roles = iRoleService.selectList(null);
        map.put("roles",roles);
        return MANAGER + "/authorities";
    }

    @GetMapping("/apiInfo")
    public String apiInfo(ModelMap map) {
        //转跳到swagger页面
        return "redirect:https://www.layui.com/";
    }
    @GetMapping("/druidInfo")
    public String druidInfo(ModelMap map) {
        //转跳到druid页面页面
        return "redirect:https://www.layui.com/";
    }
    @GetMapping("/logInfo")
    public String logInfo(ModelMap map, @AuthenticationPrincipal UserDetails userDetails) {
        //查出用户
        String username = userDetails.getUsername();
        BaseUser baseUser = iUserService.selectUserByName(username);
        //添加权限按钮
        List<MenuButton> userButton = iMenuButtonService.getUserButton(username, 6);
        addDetail(map, userButton);
        //查出日志信息
        EntityWrapper<Log> wrapper = new EntityWrapper<>();
        if (baseUser != null) {
            wrapper.eq("u_id",baseUser.getuId());
        }
        wrapper.orderBy("create_time",false);
        List<Log> logs = iLogService.selectList(wrapper);
        map.put("logs", logs);
        return MANAGER + "/logInfo";
    }
}
